package com.example.orderminiappapi.controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import com.example.orderminiappapi.bean.Response;
import com.example.orderminiappapi.tool.Createuuid;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Random;

@RestController
@CrossOrigin
public class FileController {

    @ResponseBody
    @RequestMapping(value = "/uploadMealImage", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public Response uploadMealImage(HttpServletRequest request, @RequestParam("file") MultipartFile myFile) throws IOException {

        String token = request.getHeader("tokenMiniAPP");

        JWTVerifier build = JWT.require(Algorithm.HMAC256("userInfo")).build();
        DecodedJWT verify = build.verify(token);

        String shopId = verify.getClaim("shopId").asString();


        String thisFilename = new StringBuilder().append(System.nanoTime()).append(new Random().nextInt(100)).append(".").append(myFile.getContentType().substring(myFile.getContentType().indexOf("/") + 1)).toString();

        String suffix= thisFilename.substring(thisFilename.lastIndexOf("."));
        Createuuid createuuid = new Createuuid();
        String uuid = createuuid.getOrderIdByUUId();
        String filename = uuid + suffix;

        String filePath = "D:\\毕业设计\\image\\mealLogo\\";
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String filePathWithUserId = filePath + shopId;
        file = new File(filePathWithUserId);
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(filePathWithUserId + "/" + filename);

        FileUtils.copyInputStreamToFile(myFile.getInputStream(), file);

        Response response = new Response("10000", "成功", "/" + shopId + "/" + filename);
        return response;
    }

    @ResponseBody
    @RequestMapping(value = "/uploadShopImage", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public Response uploadShopImage(HttpServletRequest request, @RequestParam("file") MultipartFile myFile) throws IOException {

        String token = request.getHeader("tokenMiniAPP");

        JWTVerifier build = JWT.require(Algorithm.HMAC256("userInfo")).build();
        DecodedJWT verify = build.verify(token);

        String shopId = verify.getClaim("shopId").asString();


        String thisFilename = new StringBuilder().append(System.nanoTime()).append(new Random().nextInt(100)).append(".").append(myFile.getContentType().substring(myFile.getContentType().indexOf("/") + 1)).toString();

        String suffix= thisFilename.substring(thisFilename.lastIndexOf("."));
        Createuuid createuuid = new Createuuid();
        String uuid = createuuid.getOrderIdByUUId();
        String filename = uuid + suffix;

        String filePath = "D:\\毕业设计\\image\\shopLogo\\";
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String filePathWithUserId = filePath + shopId;
        file = new File(filePathWithUserId);
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(filePathWithUserId + "/" + filename);

        FileUtils.copyInputStreamToFile(myFile.getInputStream(), file);

        Response response = new Response("10000", "成功", "/" + shopId + "/" + filename);
        return response;
    }
}
